import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/viewmodel/medecine/medical_advice_manage_view_model.dart';
import 'package:ls_work/widgets/customer_under_line_tab_indicator.dart';

import '../../res/gaps.dart';
import '../../widgets/common_widget.dart';
import 'fragment/medical_prescription_fragment.dart';
import 'fragment/treatment_list_fragment.dart';

class MedicalAdviceManagePage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _MedicalAdviceManageState();
}

class _MedicalAdviceManageState extends BaseMvvmState<
    MedicalAdviceManageViewModel, MedicalAdviceManagePage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget? buildLoadingContentView() {
    return DefaultTabController(
        length: 2,
        child: Column(
          children: [
            TabBar(
              tabs: [
                Tab(
                  text: '处方',
                ),
                Tab(
                  text: '治疗',
                )
              ],
              labelColor: Colours.app_main,
              unselectedLabelColor: Colors.black,
              indicator: CustomUnderlineTabIndicator(
                borderSide: BorderSide(color: Colours.app_main, width: 4),
              ),
              indicatorPadding: EdgeInsets.only(bottom: 6),
              labelStyle: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
              unselectedLabelStyle:
                  TextStyle(fontSize: 16, fontWeight: FontWeight.normal),
            ).addToContainer(color: Colors.white),
            TabBarView(children: [
              MedicalPrescriptionFragment(),
              TreatmentListFragment(),
            ]).setWeight(1)
          ],
        ));
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("医嘱管理"),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(Icons.search))
        ],
      ),
      body: loadingContentWidget,
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                    hintText: '输入长者姓名',
                    text: MedicalAdviceManageViewModel.searchName ?? "",
                    maxLines: 1,
                    textInputAction: TextInputAction.search,
                    onChanged: (str) {
                      MedicalAdviceManageViewModel.searchName = str;
                    },
                    onSubmitted: (str) {
                      viewModel.requestRefresh();
                    })
                .addToContainer(
                    decoration: BoxDecoration(
                        color: Colours.bg_color, borderRadius: 6.borderRadius),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 10,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: MedicalAdviceManageViewModel.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  MedicalAdviceManageViewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),


            SizedBox(height: 10,),

            CommonText('就诊卡号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入就诊卡号',
                text: MedicalAdviceManageViewModel.cardNo ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  MedicalAdviceManageViewModel.cardNo = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('处方号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入处方号',
                text: MedicalAdviceManageViewModel.prescriptionCode ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  MedicalAdviceManageViewModel.prescriptionCode = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('医生'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入医生姓名',
                text:MedicalAdviceManageViewModel.doctor ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  MedicalAdviceManageViewModel.doctor = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('处方日期'),
            SizedBox(
              height: 8,
            ),
            CommonText(MedicalAdviceManageViewModel.prescriptionDate,hintText: '请选择')
                .addToContainer(
              width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
                  viewModel.chooseDate();
            }),


          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(height: 10,),
        Row(
          children: [
            SizedBox(width: 16,),
            TextButton(
                    onPressed: () {
                      key.currentState?.closeEndDrawer();
                      viewModel.resetClick();
                    },
                    child: CommonText('重置',textColor: Colors.blue),
            style: ButtonStyle(
              shape: WidgetStateProperty.all(RoundedRectangleBorder(
                borderRadius: BorderRadius.horizontal(left: 20.radius),
                side: BorderSide(color: Colors.blue)
              ))
            ),)
                .setWeight(1),
            TextButton(
                    onPressed: () {
                      key.currentState?.closeEndDrawer();
                      viewModel.requestRefresh();
                    },
                    child: CommonText('确定',textColor: Colors.white),
                style: ButtonStyle(
                  backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                        borderRadius: BorderRadius.horizontal(right: 20.radius),
                        side: BorderSide(color: Colors.blue),
                    ))
                ))
                .setWeight(1),
            SizedBox(width: 16,),
          ],
        ),
        SizedBox(height: 12,)
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  MedicalAdviceManageViewModel createViewModel() {
    return MedicalAdviceManageViewModel(context);
  }

  @override
  void onRetryClick() {}
}
